package com.apes.fn.server.servicePlan.service;

import com.apes.scm.masterdata.enumeration.model.BusinessParam;
import com.apes.scm.masterdata.enumeration.repository.BusinessParamRepository;
import com.apes.fn.server.servicePlan.model.*;
import com.apes.fn.server.servicePlan.repository.*;
import com.apes.framework.api.DomainService;
import com.apes.framework.api.SpringManager;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import static java.util.stream.Collectors.groupingBy;


@Service("timingOfferService")
public class TimingOfferService extends DomainService {
    @Autowired
    private TimingOfferRepository timingOfferRepository;

    public void create(SimpleRequest request) {
        TimingOffer timingOffer = request.getO(TimingOffer.class);
        timingOffer.setNumberOfExecution(Integer.valueOf(SpringManager.getBean(BusinessParamRepository.class).findAllByQueryKey("timingCount").get(0).getValue()));
        if (timingOffer.getPartsQty() == 0) return;
        timingOfferRepository.save(timingOffer);
    }

    public void update(SimpleRequest request) {
        TimingOffer timingOffer = request.getO(TimingOffer.class);
        timingOfferRepository.save(timingOffer);
    }

    public void saveAll(List<TimingOffer> findNoExecutes) {
        timingOfferRepository.saveAll(findNoExecutes);
    }

    // 定时任务请求
    public void timingExecute() {
        List<TimingOffer> findNoExecutes = timingOfferRepository.findNoExecute();
        List<BusinessParam> findAllByQueryKeys = SpringManager.getBean(BusinessParamRepository.class).findAllByQueryKey("baturuTimingGetOffer");
        List<BusinessParam> findTimingCount = SpringManager.getBean(BusinessParamRepository.class).findAllByQueryKey("timingCount");
        findNoExecutes.forEach(timingOffer -> {
            int numberOfExecutees = timingOffer.getNumberOfExecution();

            findAllByQueryKeys.forEach(businessParam -> {
                int rangeQty = Integer.valueOf(businessParam.getValue());
                int maxRangeQty = Integer.valueOf(businessParam.getValue2());
                int mills = Integer.valueOf(businessParam.getValue3()) * this.timingCount(Integer.valueOf(findTimingCount.get(0).getValue()), numberOfExecutees);

                if (timingOffer.getPartsQty() >= rangeQty && timingOffer.getPartsQty() <= maxRangeQty && numberOfExecutees > 0) {
                    if (this.judgeDate(timingOffer.getCreateDate(), mills)) {
                        this.sendSynEvent("fn.servicePlanceService.queryInquireResultFrom", MapUtil.mapper("servicePlanId", timingOffer.getOrigin(), "belongDeptId", timingOffer.getDept().getId()));
                        timingOffer.setNumberOfExecution(timingOffer.getNumberOfExecution() - 1);
                    }
                }
            });
        });
        this.saveAll(findNoExecutes);
    }

    private int timingCount(int timingCount, int numberOfExecutes) {
        return timingCount == numberOfExecutes? 1:timingCount - numberOfExecutes;
    }

    private boolean judgeDate(Date date, int mills) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, mills);
        Date newDate = calendar.getTime();

        Date currentDate = new Date();
        return currentDate.getTime() >= newDate.getTime();
    }
}